<?php
include_once $REP_CLASS . 'Selector.php';

class Recherche {

   // Association
   private $LibelleAssoc;
   // Evenement
   private $LibelleEvent;
   // Commune
   private $Insee;
   private $Dept;
   private $RefTheme;
   private $RefGrpTheme;

   private $assocPDO;

   /**
   * Constructeur
   * @db : la connexion de la base de donn?e
   */
   function __construct($db){
      // Cr?ation de la connexion
      $this->assocPDO = $db;
   }

   /**********************************
   * Recherche association
   **********************************/
   public function rechercheAssociation($depart)  {

      $req = "SELECT a.libelle AS libassoc, a.refassociation, c.libelle AS libcommune, c.cp
             FROM commune c INNER JOIN association a ON c.insee = a.insee ";

      if (isset($this->LibelleAssoc) and $this->LibelleAssoc != "") { $req .= " AND a.libelle = '".$this->LibelleAssoc."'"; }
      if (isset($this->Insee) and $this->Insee != "") { $req .= " AND c.insee = ".$this->Insee; }
      if (isset($this->Dept) and $this->Dept != "") { $req .= " AND c.dept = ".$this->Dept; }
      if (isset($this->RefTheme) and $this->RefTheme != "") { $req .= " AND a.reftheme = '".$this->RefTheme."'"; }
      if (isset($this->RefGrpTheme) and $this->RefGrpTheme != "") { $req .= " AND a.refgroupetheme = '".$this->RefGrpTheme."'"; }

      $req .= " ORDER BY a.libelle";
      // Si multipage
      if (isset($depart) and  $depart != -1) {

         $req .= " LIMIT 10 OFFSET ".$depart;
      }
      //echo $req;
      return $this->assocPDO->query($req);
   }
   /**********************************
   * Recherche evenement
   **********************************/
   public function rechercheEvenement()  {

      $req = "SELECT e.refevenement, e.libelle
             FROM commune c INNER JOIN evenement e ON c.insee = e.insee
             INNER JOIN association a ON a.refassociation = e.refassociation ";
      if ((isset($this->RefTheme ) and $this->RefTheme != "")
            or (isset($this->RefGrpTheme) and $this->RefGrpTheme != "")) {
         $req .= "INNER JOIN lstthemeevent l ON l.refevenement = a.refevenement";
      }

      if (isset($this->LibelleEvent) and $this->LibelleEvent != "") { $req .= " AND e.libelle = '".$this->LibelleEvent."'"; }
      if (isset($this->LibelleAssoc) and $this->LibelleAssoc != "") { $req .= " AND a.libelle = '".$this->LibelleAssoc."'"; }
      if (isset($this->Insee) and $this->Insee != "") { $req .= " AND c.insee = ".$this->Insee; }
      if (isset($this->Dept) and $this->Dept != "") { $req .= " AND c.dept = ".$this->Dept; }
      if (isset($this->RefTheme) and $this->RefTheme != "") { $req .= " AND l.reftheme = '".$this->RefTheme."'"; }
      if (isset($this->RefGrpTheme) and $this->RefGrpTheme != "") { $req .= " AND l.refgroupetheme = '".$this->RefGrpTheme."'"; }

      $req .= " ORDER BY e.libelle";

      return $this->assocPDO->query($req);
   }


   /****************
   * ACCESSEURS
   *****************/
   public function setLibelleAssoc($LibelleAssoc){
     $this->LibelleAssoc = $LibelleAssoc;
   }

   public function setLibelleEvent($LibelleEvent){
     $this->LibelleEvent = $LibelleEvent;
   }

   public function setInsee($Insee){
     $this->Insee = $Insee;
   }

   public function setDept($Dept){
     $this->Dept = $Dept;
   }

   public function setRefTheme($RefTheme){
     $this->RefTheme = $RefTheme;
   }

   public function setRefGrpTheme($RefGrpTheme){
     $this->RefGrpTheme = $RefGrpTheme;
   }

}
?>